package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.PropertyParkCouponGetPageDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyParkCouponSaveDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyParkCouponUpdateDTO;
import com.itlong.cloud.POJO.PO.PropertyParkCouponPO;
import com.itlong.cloud.POJO.VO.property.PropertyParkCouponGetOneVO;
import com.itlong.cloud.POJO.VO.property.PropertyParkCouponGetPageVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.enumerate.DataStatusEnum;
import com.itlong.cloud.property.dao.IPropertyParkCouponDao;
import com.itlong.cloud.property.service.IPropertyParkCouponService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.date.DateUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.random.LogicIdUtil;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <desc>
 *      智慧停车-停车优惠券管理服务层接口实现类
 * </desc>
 *
 * @createDate 2018/11/06
 */
@Service
public class PropertyParkCouponServiceImpl implements IPropertyParkCouponService {

    @Autowired
    private IPropertyParkCouponDao iPropertyParkCouponDao;

    /**
     * <desc>
     *      获取单个优惠券信息
     * </desc>
     *
     * @param parkCouponId 优惠券id
     * @return 单个优惠券信息
     * @author wangzhi
     * @createDate 2018/11/06
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public PropertyParkCouponGetOneVO getOneCoupon(String parkCouponId) throws Exception {
        return iPropertyParkCouponDao.getOneCoupon(parkCouponId);
    }

    /**
     * <desc>
     *      分页获取优惠券
     * </desc>
     *
     * @param propertyParkCouponGetPageDTO 分页获取优惠券DTO
     * @return 分页获取优惠券结果
     * @author wangzhi
     * @createDate 2018/11/06
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyParkCouponGetPageVO> getCouponPage(PropertyParkCouponGetPageDTO propertyParkCouponGetPageDTO) throws Exception {
        Map<String,Object> params = SqlUtil.convertBeanToMapParams(propertyParkCouponGetPageDTO);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date nowTime = sdf.parse(sdf.format(new Date()));
        if (propertyParkCouponGetPageDTO.getUseStatus() != null && propertyParkCouponGetPageDTO.getUseStatus() == 3) {
            params.remove("useStatus");
            String endTime = sdf.format(nowTime);
            params.put("endTime",endTime);
        }
        if (propertyParkCouponGetPageDTO.getUseStatus() != null && propertyParkCouponGetPageDTO.getUseStatus() == 1) {
            String unusedTime = sdf.format(nowTime);
            params.put("unusedTime",unusedTime);
        }
        Page<PropertyParkCouponGetPageVO> page = new Page<>();
        page.getPage().setPageSize(propertyParkCouponGetPageDTO.getPageSize());
        page.getPage().setCurrentPage(propertyParkCouponGetPageDTO.getCurrentPage());
        params.put("startSlip", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());
        List<PropertyParkCouponGetPageVO> propertyParkCouponGetPageVOS = iPropertyParkCouponDao.getCouponPage(params);
        for(PropertyParkCouponGetPageVO propertyParkCouponGetPageVO : propertyParkCouponGetPageVOS){
            Date endTime = sdf.parse(propertyParkCouponGetPageVO.getEndTime());
            if (propertyParkCouponGetPageVO.getUseStatus() != null && propertyParkCouponGetPageVO.getUseStatus() == 1 && endTime.compareTo(nowTime) < 0) {
                propertyParkCouponGetPageVO.setUseStatus(3);
            }
        }
        page.setRows(propertyParkCouponGetPageVOS,iPropertyParkCouponDao.getCouponRecordCount(params));
        return page;
    }

    /**
     * <desc>
     *      获取项目未使用的优惠券数量
     * </desc>
     *
     * @param projectId 项目id
     * @return 项目未使用的优惠券数量
     * @author wangzhi
     * @createDate 2018/11/06
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Integer getAvailableCouponCount(String projectId) {
        return iPropertyParkCouponDao.getAvailableCouponCount(projectId);
    }

    /**
     * <desc>
     *      生成优惠券
     * </desc>
     *
     * @param propertyParkCouponSaveDTO 生成优惠券DTO
     * @return 生成的优惠券
     * @author wangzhi
     * @createDate 2018/11/06
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public List<PropertyParkCouponPO> saveCoupon(PropertyParkCouponSaveDTO propertyParkCouponSaveDTO) throws Exception {
        Date nowTime = new Date();
        List<PropertyParkCouponPO> propertyParkCouponPOList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1);
        for (int i = 1; i <= propertyParkCouponSaveDTO.getCount(); i++){
            PropertyParkCouponPO propertyParkCouponPO = new PropertyParkCouponPO();
            propertyParkCouponPO.setParkCouponId(LogicIdUtil.bussinessId());
            propertyParkCouponPO.setType(propertyParkCouponSaveDTO.getType());
            propertyParkCouponPO.setDerate(propertyParkCouponSaveDTO.getDerate());
            propertyParkCouponPO.setProjectId(propertyParkCouponSaveDTO.getProjectId());
            propertyParkCouponPO.setRedeemCode(RandomStringUtils.random(8, "abcdefghijklmnopqrstuvwxyz1234567890").toUpperCase());
            propertyParkCouponPO.setEndTime(simpleDateFormat.parse(propertyParkCouponSaveDTO.getEndTime()));
            propertyParkCouponPO.setRedeemStatus(1);
            propertyParkCouponPO.setUseStatus(1);
            propertyParkCouponPO.setStatus(DataStatusEnum.NORMAL.getType());
            propertyParkCouponPO.setCreateTime(nowTime);
            propertyParkCouponPO.setUpdateTime(nowTime);
            propertyParkCouponPOList.add(propertyParkCouponPO);
        }
        iPropertyParkCouponDao.saveCoupon(propertyParkCouponPOList);
        return propertyParkCouponPOList;
    }

    /**
     * <desc>
     *      修改优惠券
     * </desc>
     *
     * @param propertyParkCouponUpdateDTO 修改优惠券DTO
     * @return 受影响的行数
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer updateCoupon(PropertyParkCouponUpdateDTO propertyParkCouponUpdateDTO) throws Exception {
        PropertyParkCouponPO propertyParkCouponPO = new PropertyParkCouponPO();
        propertyParkCouponPO.setParkCouponId(propertyParkCouponUpdateDTO.getParkCouponId());
        propertyParkCouponPO.setType(propertyParkCouponUpdateDTO.getType());
        propertyParkCouponPO.setDerate(propertyParkCouponUpdateDTO.getDerate());
        propertyParkCouponPO.setEndTime(new SimpleDateFormat(DateUtil.DATE_FORMAT_TYPE1).parse(propertyParkCouponUpdateDTO.getEndTime()));
        propertyParkCouponPO.setUpdateTime(new Date());
        Map<String,Object> map = SqlUtil.durableData(propertyParkCouponPO, PlatformConstants.TABLE_UPDATE);
        return iPropertyParkCouponDao.update(map);
    }

    /**
     * <desc>
     *      删除优惠券
     * </desc>
     *
     * @param ids 优惠券id，多个用英文","相连
     * @return 1-成功 other-失败
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer deleteCoupons(String ids) throws Exception {
        List<String> idList = StringHandlerUtil.splitStringList(ids);
        return iPropertyParkCouponDao.deleteCoupons(idList);
    }

    /**
     * <desc>
     *      获取导出优惠券
     * </desc>
     *
     * @param propertyParkCouponGetPageDTO 查询参数
     * @return 导出优惠券集合
     * @author wangzhi
     * @createDate 2018/11/07
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<PropertyParkCouponGetPageVO> getExportData(PropertyParkCouponGetPageDTO propertyParkCouponGetPageDTO) throws Exception {
        List<PropertyParkCouponGetPageVO> propertyParkCouponGetPageVOS;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date nowTime = sdf.parse(sdf.format(new Date()));
        if (StringUtils.isNotBlank(propertyParkCouponGetPageDTO.getIds())) {
            List<String> idList = StringHandlerUtil.splitStringList(propertyParkCouponGetPageDTO.getIds());
            propertyParkCouponGetPageVOS = iPropertyParkCouponDao.getCouponsByIds(idList);
        } else {
            Map<String, Object> params = SqlUtil.convertBeanToMapParams(propertyParkCouponGetPageDTO);
            if (propertyParkCouponGetPageDTO.getUseStatus() != null && propertyParkCouponGetPageDTO.getUseStatus() == 3) {
                params.remove("useStatus");
                String endTime = sdf.format(nowTime);
                params.put("endTime",endTime);
            }
            if (propertyParkCouponGetPageDTO.getUseStatus() != null && propertyParkCouponGetPageDTO.getUseStatus() == 1) {
                String unusedTime = sdf.format(nowTime);
                params.put("unusedTime",unusedTime);
            }
            propertyParkCouponGetPageVOS = iPropertyParkCouponDao.getCouponsByCondition(params);
        }
        if (propertyParkCouponGetPageVOS != null && !propertyParkCouponGetPageVOS.isEmpty()) {
            for(PropertyParkCouponGetPageVO propertyParkCouponGetPageVO : propertyParkCouponGetPageVOS){
                Date endTime = sdf.parse(propertyParkCouponGetPageVO.getEndTime());
                if (propertyParkCouponGetPageVO.getUseStatus() != null && propertyParkCouponGetPageVO.getUseStatus() == 1 && endTime.compareTo(nowTime) < 0) {
                    propertyParkCouponGetPageVO.setUseStatus(3);
                }
            }
        }
        return propertyParkCouponGetPageVOS;
    }

}
